package com.lazy.book.service.system.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysDictTypeMapper;
import com.lazy.book.pojo.system.pojo.dto.sysdicttype.SysDictTypeCreateDTO;
import com.lazy.book.pojo.system.pojo.dto.sysdicttype.SysDictTypeListDTO;
import com.lazy.book.pojo.system.pojo.dto.sysdicttype.SysDictTypeUpdateDTO;
import com.lazy.book.pojo.system.pojo.po.SysDictType;
import com.lazy.book.service.system.SysDictTypeService;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.PageUtils;
import com.sz.core.util.Utils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.lazy.book.pojo.system.pojo.po.table.SysDictTypeTableDef.SYS_DICT_TYPE;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Override
    public void create(SysDictTypeCreateDTO dto) {
        SysDictType sysDictType = BeanCopyUtils.copy(dto, SysDictType.class);
        save(sysDictType);
    }

    @Override
    public void update(SysDictTypeUpdateDTO dto) {
        SysDictType sysDictType = BeanCopyUtils.copy(dto, SysDictType.class);
        updateById(sysDictType);
    }

    @Override
    public PageResult<SysDictType> page(SysDictTypeListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_DICT_TYPE);
        if (Utils.isNotBlank(dto.getTypeName())) {
            wrapper.and(SYS_DICT_TYPE.TYPE_NAME.like(dto.getTypeName()));
        }
        if (Utils.isNotBlank(dto.getTypeCode())) {
            wrapper.and(SYS_DICT_TYPE.TYPE_CODE.like(dto.getTypeCode()));
        }
        Page<SysDictType> page = page(dto.buildPage(), wrapper);
        return PageUtils.getPageResult(page);
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertIsFalse(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SysDictType detail(Object id) {
        return getById(id);
    }

    @Override
    public List<DictTypeVO> findDictType() {
        QueryWrapper wrapper = QueryWrapper.create().orderBy(SYS_DICT_TYPE.CREATE_TIME.desc());
        return listAs(wrapper, DictTypeVO.class);
    }

    @Override
    public List<DictTypeVO> selectDictTypeOptions() {
        return dictLoaderFactory.getAllDictType();
    }

}
